<?php

namespace App\Http\Controllers\Admin\Shop;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Admin\CommonController;
use App\Models\wechat\Order;
use App\Models\shop\Express;
use DB;
use Carbon;

class OrderController extends CommonController
{
    public function __construct()
    {
        parent::__construct();
        view()->share([
            '_system' => 'shop',
            '_order' => 'am-active',
            '_shop' => 'am-in',
            'order_status' => config('fangshop.order_status'),
        ]);
    }

    public function index(Request $request)
    {

        //多条件查找
        $where = function ($query) use ($request) {
            if ($request->has('id') and $request->id != '') {
                $query->where('id', $request->id);
            }

            if ($request->has('customer_id') and $request->customer_id != '') {
                $query->where('customer_id', $request->customer_id);
            }

            if ($request->has('total_price') and $request->total_price != '') {

                $status = is_numeric($request->total_price) ? '=' : substr($request->total_price, 0, 1);
                $total_price = substr($request->total_price, 1);

                switch ($status) {
                    case '>':
                        $query->where('total_price', '>=', $total_price);
                        break;
                    case '<' :
                        $query->where('total_price', '<=', $total_price);
                        break;
                    //用户直接输入的是金额,那么就等于
                    default:
                        $query->where('total_price', $request->total_price);
                }
            }

            if ($request->has('status') and $request->status != '-1') {
                $query->where('status', $request->status);
            }

            if ($request->has('created_at') and $request->created_at != '') {
                $time = explode(" ~ ", $request->input('created_at'));
                foreach ($time as $k => $v) {
                    $time["$k"] = $k == 0 ? $v . " 00:00:00" : $v . " 23:59:59";
                }
                $query->whereBetween('created_at', $time);
            }
        };

        $orders = Order::where($where)
            ->with('order_products.product', 'customer', 'address')
            ->orderBy('created_at', 'desc')
            ->paginate(config('fangshop.page_size'));

//        return $orders;

        return view('admin.shop.orders.index')->with('orders', $orders);
    }


    public function show($id)
    {
        $expresses = Express::orderBy('sort_order')->get();
        $order = Order::with('address', 'express', 'customer', 'order_products.product')->find($id);
//        return $order;
        return view('admin.shop.orders.show')
            ->with('order', $order)
            ->with('expresses', $expresses);
    }


    //配货 修改订单状态
    public function picking(Request $request)
    {
        $order = Order::find($request->id);
        $order->status = 3;
        $order->picking_time = Carbon\Carbon::now();
        $order->save();
    }

    //配货 修改发货信息
    //发货
    function shipping(Request $request)
    {
        $order = Order::find($request->id);
        if ($request->status == 3) {
            $order->status = 4;
            $order->shipping_time = Carbon\Carbon::now();
        }
        $order->express_code = $request->express_code;
        $order->express_id = $request->express_id;

        $order->save();
    }


    //交易成功 修改订单状态
    public function finish(Request $request)
    {
        $order = Order::find($request->id);
        $order->status = 5;
        $order->finish_time = Carbon\Carbon::now();
        $order->save();
    }



    //确认支付  //此功能没做 参考刘老师的案例
    function order_notify()
    {
        $payment = EasyWeChat::payment();
        $response = $payment->handleNotify(function ($notify, $successful) {

            $order = Order::find($notify->out_trade_no);
//            Log::debug($notify->out_trade_no);
            if (!$order) { // 如果订单不存在
                return 'Order not exist.'; // 告诉微信，我已经处理完了，订单没找到，别再通知我了
            }

            // 如果订单存在
            // 检查订单是否已经更新过支付状态
            if ($order->status >= 2) { // 状态 >= 2 代表已经支付
                return true; // 已经支付成功了就不再更新了
            }

            // 用户是否支付成功
            if ($successful) {
                // 不是已经支付状态则修改为已经支付状态
                $order->pay_time = Carbon\Carbon::now(); // 更新支付时间为当前时间
                $order->status = 2;
                $order->save(); // 保存订单
            } else { //
                $order->status = 1;
            }

            $order->save(); // 保存订单
            return true; // 返回处理完成

        });
        return $response;
    }


}
